<%@ Page Language="VB" MasterPageFile="~/Default.master" Strict="false" Title="Untitled Page" ValidateRequest="false" %>
<%@ Register TagPrefix="Club" TagName="LoginBanner" Src="~/UserControls/LoginBanner.ascx" %>
<%@ Register TagPrefix="Club" TagName="ImageThumbnail" Src="~/UserControls/ImageThumbnail.ascx" %>
<%@ Register TagPrefix="Club" TagName="Photopicker" Src="~/UserControls/photos_formpicker.ascx" %>
<%@ Register TagPrefix="Club" TagName="Locationpicker" Src="~/UserControls/Locations_picker.ascx" %>

<script runat="server">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Session("FCKeditor:UserFilesPath") = Request.ApplicationPath.ToString & "Files/"
        If Not IsPostBack Then
            Photopicker1.ImageId = Nothing
            tp1.SelectedTime = Now
            tp2.SelectedTime = Now.AddHours(3)
            startdte.SelectedDate = Now
            enddte.SelectedDate = Now.AddHours(3)
            staticURLTextBox.Enabled = False
        End If
    End Sub
    
    Private Function CheckIfDatesValid() As Boolean
        Select Case CaseList.SelectedValue
            Case "Every Day" : Return True
            Case "Every Sunday"
                If CheckStart(DayOfWeek.Sunday) = True AndAlso CheckEnd(DayOfWeek.Sunday) = True Then
                    Return True
                Else
                    Return False
                End If
            Case "Every Monday"
                If CheckStart(DayOfWeek.Monday) = True AndAlso CheckEnd(DayOfWeek.Monday) = True Then
                    Return True
                Else
                    Return False
                End If
            Case "Every Tuesday"
                If CheckStart(DayOfWeek.Tuesday) = True AndAlso CheckEnd(DayOfWeek.Tuesday) = True Then
                    Return True
                Else
                    Return False
                End If
            Case "Every Wednesday"
                If CheckStart(DayOfWeek.Wednesday) = True AndAlso CheckEnd(DayOfWeek.Wednesday) = True Then
                    Return True
                Else
                    Return False
                End If
            Case "Every Thursday"
                If CheckStart(DayOfWeek.Thursday) = True AndAlso CheckEnd(DayOfWeek.Thursday) = True Then
                    Return True
                Else
                    Return False
                End If
            Case "Every Friday"
                If CheckStart(DayOfWeek.Friday) = True AndAlso CheckEnd(DayOfWeek.Friday) = True Then
                    Return True
                Else
                    Return False
                End If
            Case "Every Saturday"
                If CheckStart(DayOfWeek.Saturday) = True AndAlso CheckEnd(DayOfWeek.Saturday) = True Then
                    Return True
                Else
                    Return False
                End If
        End Select
    End Function

    Private Function CheckStart(ByVal dow As DayOfWeek) As Boolean
        If startdte.SelectedDate.DayOfWeek = dow Then
            Return True
        Else
            Return False
        End If
    End Function
    
    Private Function CheckEnd(ByVal dow As DayOfWeek) As Boolean
        If enddte.SelectedDate.DayOfWeek = dow Then
            Return True
        Else
            Return False
        End If
    End Function
    
    Protected Sub apply1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If CheckIfDatesValid() = True Then
            Dim description As String = ""
            If FCKeditor1.Value.ToString() = "" Then
                description = "<p></p>"
            Else
                description = FCKeditor1.Value.ToString()
            End If
        
            Dim locationname As String
            If IsDBNull(LocationNameTXT.Text) Then
                locationname = ""
            Else
                locationname = LocationNameTXT.Text
            End If
        
            Dim pictureid As Integer
            pictureid = Photopicker1.ImageId
        
            Dim maxattn As Integer
            If txtMaxAttendees.Text.ToString() = "" Then
                maxattn = 0
            Else
                maxattn = CInt(txtMaxAttendees.Text.ToString())
            End If
                
        
            Dim URL As String = ""
            If CheckBox1.Checked = True Then
                URL = staticURLTextBox.Text.ToString()
            End If
        
            Dim locdirections As String
            If FCKeditor2.Value.ToString() = "" Then
                locdirections = "<p></p>"
            Else
                locdirections = FCKeditor2.Value.ToString()
            End If
                       
            Dim datetoadd As DateTime = Nothing

            Dim finaldate As DateTime = enddte.SelectedDate
            
            Dim IsDone As Boolean = False
            
            Do Until IsDone = True
                If Not datetoadd <> Nothing Then
                    datetoadd = startdte.SelectedDate
                End If
                Dim tstarttime As DateTime = datetoadd.Add(tp1.SelectedTime.TimeOfDay)
                Dim tendtime As DateTime = datetoadd.Add(tp2.SelectedTime.TimeOfDay)
                ClubStarterKit.Data.ClubEvent.Insert(tstarttime, tendtime, titleTextBox.Text.ToString(), description, URL, pictureid, 0, chkRSVP1.Checked, maxattn, 0, LocationNameTXT.Text, locdirections)
            
                If datetoadd = finaldate Then
                    IsDone = True
                Else
                    Dim tempstorage As DateTime = datetoadd
                    If CaseList.SelectedValue = "Every Day" Then
                        datetoadd = tempstorage.AddDays(1)
                    Else
                        datetoadd = tempstorage.AddDays(7)
                    End If
                End If
            Loop
            
            Response.Redirect("list.aspx")
        Else
            status.Text = "The 'Start Date' or 'End Date' is not a day you specified for the reocurring event."
            status.ForeColor = Drawing.Color.Red
            status.Font.Bold = True
        End If
    End Sub

    Protected Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        If CheckBox1.Checked = True Then
            staticURLTextBox.Enabled = True
        Else
            staticURLTextBox.Enabled = False
        End If
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="body">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
        <Club:LoginBanner ID="LoginBanner1" runat="server" />
        <div class="fullwidth">
            <h3>Add Reocurring Event</h3>
            <div class="dashedline">
            </div>
            <table>
                        <tr>
                            <td class="formlabel">
                                Event Heading:
                            </td>
                            <td align="left">
                                <asp:TextBox ID="titleTextBox" runat="server" Width="500px" Text=""></asp:TextBox>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="titleTextBox"
                                    ErrorMessage="Please Enter a Heading." ValidationGroup="add" ToolTip="Please Enter a Heading.">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Link:
                            </td>
                            <td align="left">
                                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:CheckBox ID="CheckBox1" runat="server" Text="Use a link instead of inline content for this anouncement:" OnCheckedChanged="CheckBox1_CheckedChanged" /><br />
                                    Link:
                                    <asp:TextBox runat="server" ID="staticURLTextBox"
                                        Width="450px" Enabled="False" />
                                </ContentTemplate>
                                </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Location Name:
                            </td>
                            <td align="left">
                                <asp:TextBox ID="LocationNameTXT" runat="server" Width="500px" Text=""></asp:TextBox>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Location Description:
                            </td>
                            <td align="left">
                                <FCKeditorV2:FCKeditor ID="FCKeditor2" runat="server" ToolbarSet="Default" SkinPath="skins/office2003/"
                                    Value="" BasePath="~/FCKeditor/" UseBROnCarriageReturn="true">
                                </FCKeditorV2:FCKeditor>                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                <asp:Label ID="Label1" runat="server" Text="Description:" />
                            </td>
                            <td align="left">
                                <FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server" ToolbarSet="Default" SkinPath="skins/office2003/"
                                    Value="" BasePath="~/FCKeditor/" UseBROnCarriageReturn="true">
                                </FCKeditorV2:FCKeditor>
                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FCKeditor1"
                                    ErrorMessage="Please Enter a Description." ValidationGroup="add" EnableClientScript="False" ToolTip="Please Enter a Heading.">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Event Date:</td>
                            <td align="left">
                            <asp:UpdatePanel ID="up1" runat="server"><ContentTemplate>
                                <table>
                                    <tr>
                                        <td>
                                            Start Time:
                                        </td>
                                        <td>
                                            <Club:TimePicker ID="tp1" runat="server" />
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            End Time:
                                        </td>
                                        <td>
                                            <Club:TimePicker ID="tp2" runat="server" />
                                        </td>
                                    </tr>
                                    <tr></tr>
                                    <tr>
                                        <td>
                                        Start Date:
                                        </td>
                                        <td>
                                            <Club:DatePicker ID="startdte" runat="server" /></td>
                                    </tr>
                                    <tr>
                                        <td>
                                            End Date:</td>
                                        <td>
                                            <Club:DatePicker ID="enddte" runat="server" />
                                        </td>
                                    </tr>
                                    <tr>
                                    <td>
                                        Reocurrance Setting: <asp:DropDownList ID="CaseList" runat="server" >
                                            <asp:ListItem>Every Day</asp:ListItem>
                                            <asp:ListItem>Every Sunday</asp:ListItem>
                                            <asp:ListItem>Every Monday</asp:ListItem>
                                            <asp:ListItem>Every Tuesday</asp:ListItem>
                                            <asp:ListItem>Every Wednesday</asp:ListItem>
                                            <asp:ListItem>Every Thursday</asp:ListItem>
                                            <asp:ListItem>Every Friday</asp:ListItem>
                                            <asp:ListItem>Every Saturday</asp:ListItem>
                                        </asp:DropDownList>&nbsp;
                                    </td>
                                    </tr>
                                </table>
                            </ContentTemplate></asp:UpdatePanel>
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Photo:</td>
                            <td align="left">
                            <asp:UpdatePanel ID="UpdatePanel2" runat="server">
                                <ContentTemplate>
                                <Club:Photopicker ID="Photopicker1" runat="server" />
                                </ContentTemplate>
                            </asp:UpdatePanel>
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel">
                                Want online sign-ups?</td>
                            <td align="left">
                                <asp:CheckBox runat="server" Checked="false" ID="chkRSVP1" />
                            </td>
                        </tr>
                        <tr>
                            <td class="formlabel" style="height: 67px">
                                Maximum attendees:<br />
                                (For Unlimited, type -1)
                            </td>
                            <td style="height: 67px">
                                <asp:TextBox ID="txtMaxAttendees" runat="server" Width="50px" Text="" ></asp:TextBox>
                            </td>
                        </tr>
                    </table>
                    <div class="dashedline">
                    </div>
                    <div class="actionbuttons">
                        <Club:RolloverButton ID="apply1" Text="Add Event" runat="server" ValidationGroup="add" OnClick="apply1_Click" />
                        <Club:RolloverLink ID="Cancel" Text="Cancel" runat="server" NavigateURL="Calendar.aspx" />
                    </div>
                    <asp:Label ID="status" runat="server" />
        </div>
</div>
</asp:Content>

